package com.paul.controller.inventoryitem;

import javax.ejb.EJB;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.paul.inventoryitem.dao.InventoryItemCombineDao;
import com.paul.inventoryitem.dao.InventoryItemDao;
import com.paul.inventoryitem.valueobject.InventoryItem;
 
@Controller
public class InventoryItemController {
	
	 private static Log log = LogFactory.getLog(InventoryItemController.class);  
	
	@EJB(mappedName="java:global/javaee6-ear-1.0.0/javaee6-ejb-1.0.0/InventoryItemDao") 
    private InventoryItemDao inventoryItemDao;
	
	@EJB(mappedName="java:global/javaee6-ear-1.0.0/javaee6-ejb-1.0.0/InventoryItemCombineDao") 
    private InventoryItemCombineDao inventoryItemCombineDao;
 
	@RequestMapping(method = RequestMethod.GET, value = "/welcome")
	public String printWelcome(ModelMap model) {
		
		model.addAttribute("message", inventoryItemDao.find(Integer.valueOf(1)).getName());
		return "helloworld";
 
	}
	
	@RequestMapping("/inventoryItem/add-page")  
    public ModelAndView addInventorySetup(InventoryItem item){  
          
        log.info("setup add inventory");  
        return new ModelAndView("/inventoryItem/inventoryItem-add", "item", item);  
    }  
	
	@RequestMapping(value="/inventoryItem/save", method=RequestMethod.POST)  
    public String processInventoryMeta(  
            @ModelAttribute("item") @Valid InventoryItem item,   
            BindingResult result) {  
          
        log.info("process add inventory");  
          
        if (result.hasErrors()) {  
            return "/inventoryItem/inventoryItem-add";  
        }  
  
        inventoryItemCombineDao.testTraction(item);
//        inventoryItemDao.createWithoutException(item);  
//        inventoryItemDao.createWithException(item);  
//        inventoryItemDao.createWithException(item);  
          
        return "redirect:/add/images/"+item.getId();  
    }  
 
}